﻿using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using YArchitech.Revit;

namespace YJKPresentation.CommonUnit
{
	public class ParameterCopier
	{
		public static void CopyParameters(Element element, ref Element newElement)
		{
			ParameterCopier.CopyParametersBasic(element, false, ref newElement);
		}

		public static void CopyParameters(Element element, bool isCrossFloor, ref Element newElement)
		{
			ParameterCopier.CopyParametersBasic(element, isCrossFloor, ref newElement);
		}

		private static void CopyParametersBasic(Element element, bool isCrossFloor, ref Element newElement)
		{
			foreach (object obj in element.Parameters)
			{
				Parameter parameter = (Parameter)obj;
				if (!parameter.IsReadOnly && parameter.HasValue)
				{
					try
					{
						if (isCrossFloor && parameter.Definition.GetType() == typeof(InternalDefinition))
						{
							InternalDefinition internalDefinition = parameter.Definition as InternalDefinition;
                            if ((int)internalDefinition.BuiltInParameter == -1001352 || (int)internalDefinition.BuiltInParameter == -1001360)
							{
								continue;
							}
						}
                        switch ((int)parameter.StorageType)
						{
						case 1:
							newElement.GetParameter(parameter.Definition.Name).Set(parameter.AsInteger());
							break;
						case 2:
							newElement.GetParameter(parameter.Definition.Name).Set(parameter.AsDouble());
							break;
						case 3:
							newElement.GetParameter(parameter.Definition.Name).Set(parameter.AsString());
							break;
						}
					}
					catch (Exception)
					{
					}
				}
			}
		}

		public static void CopyParameters(FamilyInstance instance, ref FamilyInstance newInstance)
		{
			Element element = newInstance;
			ParameterCopier.CopyParametersBasic(instance, false, ref element);
		}

		public static void CopyParameters(FamilyInstance instance, bool isCrossFloor, ref FamilyInstance newInstance)
		{
			Element element = newInstance;
			ParameterCopier.CopyParametersBasic(instance, isCrossFloor, ref element);
		}

		public static void CopyParameters(Wire wire, ref Wire newWire)
		{
			Element element = newWire;
			ParameterCopier.CopyParameters(wire, false, ref element);
		}

		public static bool GetSillHeightParam(FamilyInstance instance, ref double value)
		{
			Parameter parameter = instance.get_Parameter(BuiltInParameter.INSTANCE_SILL_HEIGHT_PARAM);
			if (parameter == null)
			{
				return false;
			}
			value = parameter.AsDouble();
			return true;
		}

		public static void SetSillHeightParam(ref FamilyInstance instance, double value)
		{
			instance.get_Parameter(BuiltInParameter.INSTANCE_SILL_HEIGHT_PARAM).Set(value);
		}
	}
}
